<template>
    <div>
        <div>
            <el-button type="primary" @click="dialogFormVisible = true; operataType = 'save'"
                style="margin-bottom: 20px;">添加销售商品</el-button>
            <el-input placeholder="请输入销售id号" v-model="Byid" style="width: 300px; left: 15px" 
            suffix-icon="el-icon-search" @keyup.enter.native="findById">
                <el-button slot="append" icon="el-icon-search" @click="findById"></el-button>
            </el-input>
        </div>
        <el-dialog title="商品信息" :visible.sync="dialogFormVisible" @close="handleClose">
            <el-form :model="saless">
                <el-form-item label="商品销售编号" :label-width="formLabelWidth">
                    <el-input v-model="saless.id" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="商品编号" :label-width="formLabelWidth">
                    <el-input v-model="saless.product_id" autocomplete="off"></el-input>
                </el-form-item>
                <!-- <el-form-item label="商品销售数量" :label-width="formLabelWidth">
                    <el-input v-model="saless.sales_num" autocomplete="off"></el-input>
                </el-form-item> -->

                <el-form-item label="商品销售数量" :label-width="formLabelWidth" prop="sales_num" :rules="[
                    { required: true, message: '商品销售数量不能为空' }]">
                    <el-input-number v-model="saless.sales_num" :min="0" label="商品销售数量"></el-input-number>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleSave">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 数据表格 -->
        <el-table :data="sales" style="width: 100%">
            <el-table-column type="selection" width="55"></el-table-column>
            <el-table-column label="商品销售编号" :width="tableLabelWidth">
                <template slot-scope="scope">
                    <td>{{ scope.row.id }}</td>
                </template>
            </el-table-column>
            <el-table-column label="商品编号" :width="tableLabelWidth">
                <template slot-scope="scope">
                    <td>{{ scope.row.product_id }}</td>
                </template>
            </el-table-column>
            <el-table-column label="商品销售数量" :width="tableLabelWidth">
                <template slot-scope="scope">
                    <td>{{ scope.row.sales_num }}</td>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-pagination background layout="prev, pager, next" :total="total" :page-size="pageSize"
            :current-page="current" @current-change="load">
        </el-pagination>
    </div>
</template>
<script>
export default {
    name: "SalesView",
    data() {
        return {
            //购物车商品列表
            sales: [],
            dialogFormVisible: false, //控制对话框的显示
            formLabelWidth: '120px',
            tableLabelWidth: '300px',
            saless: { id: '', product_id: '', sales_num: '' },//用户数据
            operataType: '',//操作类型  save  update
            total: 0,//用户总数
            pageSize: 5,//每页行数
            current: 0,//当前页数
            Byid:"",//模糊查询
        }
    },
    methods: {
        //加载函数
        load(current) {
            //分页
            this.current = current
            //调用后台的方法，参数是url
            this.$http.get('sales?action=page&current=' + this.current + "&pageSize=" + this.pageSize)
                .then(resp => {
                    //处理后台返回的结果
                    console.log(resp.data)
                    //数据绑定
                    if (resp.data.data) {
                        this.sales = resp.data.data.records
                        this.total = resp.data.data.total
                    }
                })
        },
        handleEdit(index, row) {
            this.operataType = 'update';
            //显示对话框，绑定数据
            this.dialogFormVisible = true;
            //进行深克隆 row-->Strign-->json
            this.saless = JSON.parse(JSON.stringify(row));
        },
        //保存数据操作
        handleSave() {
            if (this.operataType == 'save') {
                //添加用户操作
                console.log('save s_sales')
                this.$http.post('/sales?action=save', this.$qs.stringify(this.saless))
                    .then(resp => {
                        console.log('save s_sales', resp.data)
                        if (resp.data.data == true) {
                            this.load(this.current);
                        }
                    })
            } else {
                //更新用户操作
                this.$http.post('/sales?action=update', this.$qs.stringify(this.saless))
                    .then(resp => {
                        console.log('update s_sales', resp.data)
                        if (resp.data.data == true) {
                            this.load(this.current);
                        }
                    })
            }
            this.dialogFormVisible = false
        },
        handleDelete(index, row) {
            this.$confirm('是否确定删除?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.post('/sales?action=delete&id=' + row.id).then(resp => {
                    if (resp.data.data == true) {
                        this.load(this.current);
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        });
                    }
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //根据销售id查询商品
        findById() {
            this.$http.post('/sales?action=findByIdContaining&id=' + this.Byid).then(resp => {
                if(resp.data.data) {
                    console.log('lalal',this.data)
                    this.total = resp.data.data.length
                    this.sales = resp.data.data
                    this.$message({
                        type: 'success',
                        message: '查询成功!'
                    });
                }
            })
        },
        //对话框关闭
        handleClose() {
            this.saless = { id: '', product_id: '', sales_num: '' },//用户数据
                this.operataType = ''
        }
    },
    //页面挂载钩子
    mounted() {
        this.load(1)
    }
}

</script>

<style>
.el-table .warning-row {
    background: oldlace;
}

.el-table .success-row {
    background: #f0f9eb;
}
</style>